package com.rd.student.jike.class1.fangzhou.day0728;

import java.util.*;

public class Test01 {
    public static void main(String[] args) {
        String stu = "郑成祥、王博、孙文卓、郑睿娴、王高博、李春泉、张睿、王星凯、柏云耀、高艺铭、周子贺、杜航、高晨曦、张思韬、孙昊、方周、郭佳宝、姜万里、李宏鸣、薛志鑫、王海鑫、夏银银、吴俊豪、王向阳、徐铭泽、张世煊、才雨鑫、孙国伟、秦嘉林、王岩博、石睿、杜雨坤、黄奕鸣、金洪宇、李鑫、王宇芳、苏岩";
        List<String> student =  Arrays.asList(stu.split("、"));
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要随机打印出来的学生的个数：");
        int n = sc.nextInt();
        System.out.println(pickRandom(student,n));
    }
    public static List<String> pickRandom(List<String> student, int n) {
        if (student.size() < n) {
            throw new IllegalArgumentException("n 大于班级总人数");
        }

        List<String> result = new ArrayList<>();
        Random random = new Random();
        int[]arr = new int [student.size()];
        for (int i = 0; i < n;) {
            int a = 0;
            int index = random.nextInt(student.size());
            for(int j = 0; j < i ;j++){
                if(index == arr[j]){
                    a = 1;
                }
            }
            if(a == 0) {
                String name = student.get(index);
                result.add(name);
                arr[i] = index;
                i++;
            }
        }

        return result;
    }
}
